#include "Main.h"
#include "Include/link_pub.h"
#include "Include/data_io_pub.h"
#include "Include/student_pub.h"
#include "Include/course_pub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Student* student_list = NULL;
Course* course_list = NULL;

void print_menu() {
    printf("\n=== Student Course Management System ===\n");
    printf("1: Student Operations\n");
    printf("2: Course Operations\n");
    printf("3: Enrollment Operations\n");
    printf("4: Exit\n");
    printf("Choose an option: ");
}

void handle_student_operations() {
    int choice;
    char id[20], name[50], new_name[50];
    Student* student;
    
    printf("1. Add Student\n");
    printf("2. Delete Student\n");
    printf("3. Modify Student\n");
    printf("4. Find Student by ID\n");
    printf("5. Find Student by Name\n");
    printf("6. Print All Students\n");
    printf("Enter choice: ");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            printf("Enter student ID: ");
            scanf("%s", id);
            printf("Enter student name: ");
            scanf("%s", name);
            student = create_student(id, name);
            student_list = add_student(student_list, student);
            log_operation("ADD_STUDENT", id);
            printf("Student added successfully\n");
            break;
        case 2:
            printf("Enter student ID to delete: ");
            scanf("%s", id);
            student_list = delete_student(student_list, course_list, id);
            log_operation("DELETE_STUDENT", id);
            printf("Student deleted\n");
            #ifdef DATABASE_INPUT
                // 保存更改到数据库
                save_students_to_db(student_list);
                save_courses_to_db(course_list);
                save_enrollments_to_db(student_list);
            #endif
            break;
        case 3:
            printf("Enter student ID to modify: ");
            scanf("%s", id);
            printf("Enter new name: ");
            scanf("%s", new_name);
            student_list = modify_student(student_list, id, new_name);
            printf("Student modified\n");
            break;
        case 4:
            printf("Enter student ID: ");
            scanf("%s", id);
            student = find_student(student_list, id);
            print_student(student);
            break;
        case 5:
            printf("Enter student name: ");
            scanf("%s", name);
            student = student_list;
            int found = 0;
            while (student) {
                if (strcmp(student->name, name) == 0) {
                    print_student(student);
                    found = 1;
                }
                student = student->next;
            }
            if (!found) {
                printf("Student with name '%s' not found\n", name);
            }
            break;
        case 6:
            print_all_students(student_list);
            break;
        default:
            printf("Invalid choice\n");
    }
}

void handle_course_operations() {
    int choice;
    char id[20], name[100], type[20];
    int hours, credits, max_capacity;
    Course* course;
    
    printf("1. Add Course\n");
    printf("2. Delete Course\n");
    printf("3. Modify Course\n");
    printf("4. Find Course\n");
    printf("5. Print All Courses\n");
    printf("Enter choice: ");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            printf("Enter course ID: ");
            scanf("%s", id);
            printf("Enter course name: ");
            scanf("%s", name);
            printf("Enter course type (required/elective): ");
            scanf("%s", type);
            printf("Enter teaching hours: ");
            scanf("%d", &hours);
            printf("Enter credits: ");
            scanf("%d", &credits);
            printf("Enter max capacity: ");
            scanf("%d", &max_capacity);
            course = create_course(id, name, type, hours, credits, max_capacity);
            course_list = add_course(course_list, course);
            log_operation("ADD_COURSE", id);
            printf("Course added successfully\n");
            break;
        case 2:
            printf("Enter course ID to delete: ");
            scanf("%s", id);
            course_list = delete_course(course_list, student_list, id);
            log_operation("DELETE_COURSE", id);
            printf("Course deleted\n");
            #ifdef DATABASE_INPUT
                // 保存更改到数据库
                save_students_to_db(student_list);
                save_courses_to_db(course_list);
                save_enrollments_to_db(student_list);
            #endif
            break;
        case 3:
            printf("Enter course ID to modify: ");
            scanf("%s", id);
            printf("Enter new name (or press Enter to skip): ");
            scanf("%s", name);
            if (strcmp(name, "") != 0 && strcmp(name, "\n") != 0) {
                printf("Enter new type (or press Enter to skip): ");
                scanf("%s", type);
                printf("Enter new hours (or -1 to skip): ");
                scanf("%d", &hours);
                printf("Enter new credits (or -1 to skip): ");
                scanf("%d", &credits);
                printf("Enter new capacity (or -1 to skip): ");
                scanf("%d", &max_capacity);
                course_list = modify_course(course_list, id, name, type, 
                                          hours == -1 ? -1 : hours,
                                          credits == -1 ? -1 : credits,
                                          max_capacity == -1 ? -1 : max_capacity);
                printf("Course modified\n");
            } else {
                printf("No changes made\n");
            }
            break;
        case 4:
            printf("Enter course ID: ");
            scanf("%s", id);
            course = find_course(course_list, id);
            print_course(course);
            break;
        case 5:
            print_all_courses(course_list);
            break;
        default:
            printf("Invalid choice\n");
    }
}

void handle_enrollment_operations() {
    int choice;
    char student_id[20], course_id[20];
    Student* student;
    Course* course;
    
    printf("1. Enroll Student in Course\n");
    printf("2. Drop Course\n");
    printf("Enter choice: ");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            printf("Enter student ID: ");
            scanf("%s", student_id);
            printf("Enter course ID: ");
            scanf("%s", course_id);
            student = find_student(student_list, student_id);
            course = find_course(course_list, course_id);
            if (student && course) {
                int result = select_course(student, course);
                switch (result) {
                    case 0:
                        printf("Student enrolled successfully\n");
                        break;
                    case -2:
                        printf("Student already enrolled in this course\n");
                        break;
                    case -3:
                        printf("Course is full\n");
                        break;
                    default:
                        printf("Failed to enroll student\n");
                        break;
                }
            } else {
                printf("Student or course not found\n");
            }
            break;
        case 2:
            printf("Enter student ID: ");
            scanf("%s", student_id);
            printf("Enter course ID: ");
            scanf("%s", course_id);
            student = find_student(student_list, student_id);
            course = find_course(course_list, course_id);
            if (student && course) {
                int result = drop_course(student, course);
                if (result == 0) {
                    printf("Course dropped successfully\n");
                } else {
                    printf("Failed to drop course\n");
                }
            } else {
                printf("Student or course not found\n");
            }
            break;
        default:
            printf("Invalid choice\n");
    }
}

int main(int argc, char* argv[]) {
    printf("Student Course Management System\n");
    
    #ifdef DATABASE_INPUT
        if (init_database() != 0) {
            printf("Database initialization failed, using file mode\n");
        } else {
            printf("Loading data from database...\n");
            student_list = load_students_from_db();
            course_list = load_courses_from_db();
         }
    #elif defined(CONSOLE_INPUT)
        printf("Starting with empty lists, use console to add data\n");
    #else
        printf("Starting with empty lists\n");
    #endif
    
    int choice;
    while (1) {
        print_menu();
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                handle_student_operations();
                break;
            case 2:
                handle_course_operations();
                break;
            case 3:
                handle_enrollment_operations();
                break;
            case 4:
                #ifdef DATABASE_INPUT
                    save_students_to_db(student_list);
                    save_courses_to_db(course_list);
                    save_enrollments_to_db(student_list);
                    close_database();
                #endif
                printf("Goodbye!\n");
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }
    
    return 0;
}